<template>
	<view>
		<!-- 空白页  没有任何数据情况下显示 -->
		<view class="" v-if="totalCount <= 0" style="display: flex;align-items: center;justify-content: center;">
			<view class="ls">
				<image class="ml" src="@/static/img/user/kong.png" mode="widthFix"
					style="width: 286rpx;height: 291upx;">
				</image>
				<view class="fs28" style="margin-top: 10upx;">暂时还没有浏览商品</view>
				<button type="default" class="btn" @tap="goIndex">去逛一逛</button>
			</view>
		</view>

		<view class="" v-else>
			<view class="bg" v-for="item in goodsList" :key="item.id">
				<view class="d-flex">
					<view class="list d-flex" @tap="goDetail(item.product_id)" style="width: 160upx;height:160upx;">
						<image class="list-image" :src="item.goods_ico" mode="scaleToFill"
							style="width: 100%;height: 100%;"></image>
					</view>
					<view class="list-text d-flex flex-column j-sa" @tap="goDetail(item.product_id)">
						<view class="">
							<view class="title">{{item.goods_name}}</view>
							<view class="Subheading">{{item.goods_introduct}}</view>
							<view class="price">￥{{item.member_price}}</view>
						</view>
						<view class="d-flex ">

						</view>
					</view>
					<view class="">
						<image @tap="delBrowser(item.id)" class="hide" src="@/static/img/cate/false.png" mode="widthFix"
							style="width: 20rpx;">
						</image>
						<view class="btn5" @tap='addCart(item.product_id)'>加入购物车</view>
					</view>
				</view>
			</view>
			<uni-load-more :status="loadingType"></uni-load-more>
		</view>
	</view>
</template>

<script>
	import {
		mapState,
		mapMutations
	} from 'vuex';
	export default {
		data() {
			return {
				page: 1,
				totalCount: 0,
				goodsList: [],
				loadingType: 'more'
			}
		},
		computed: {
			...mapState(['hasLogin', 'userInfo']),
		},
		// 页面生命周期，下拉刷新后触发
		onPullDownRefresh() {
			this.goodsList = []
			this.page = 1
			this.getNewData('refresh')
		},
		//到底获取新的数据
		onReachBottom() {
			if (!this.hasLogin) {
				this.islogin = true;
			} else {
				this.getNewData()
			}
		},
		onLoad() {
			//判断是否登录
			let result = this.$api.isLogin();
			if (result === false) {
				return false;
			}

			this.page = 1;
			this.getNewData();

		},
		methods: {

			async goDetail(id) {
				uni.navigateTo({
					url: '/pages/goods/detail?id=' + id
				})
			},
			//职位列表
			async getNewData(type = "add") {
				if (this.loadingType == 'nomore' && type == "add") {
					return false;
				}
				uni.showLoading({
					title: '加载中...'
				});

				let token = uni.getStorageSync('token');
				let dataObj = {
					page: this.page,
					token: token
				}

				let jdata = await this.$myRequest({
					url: '/shopBrowser',
					data: dataObj,
					methods: 'POST'
				})

				let ndata = jdata.data.data.data;
				if (ndata) {
					ndata = ndata.map((item) => {
						item.isChecked = true;
						return item;
					})
				}
				this.totalCount = jdata.data.data.totalCount;

				if (this.page === 1) {
					this.goodsList = ndata;
					if (ndata.length === 0) {
						this.$api.msg('亲！暂无数据！！！')
					} else {
						this.page = this.page + 1;
					}
				} else {
					if (ndata.length === 0) {
						if (this.goodsList.length === 0) {
							this.$api.msg('亲！暂无数据！！！')
						} else {
							this.$api.msg('亲！已经到底了！！！')
						}

					} else {
						this.goodsList = [...this.goodsList, ...ndata];
						this.page = this.page + 1;
					}
				}

				if (this.totalCount > this.goodsList.length) {
					this.loadingType = 'more'
				} else {
					this.loadingType = 'nomore'
				}

				uni.hideLoading();
				if (type == 'refresh') {
					uni.stopPullDownRefresh()
				}
			},

			async goIndex() {
				uni.switchTab({
					url: '/pages/cate/cate'
				})
			},

			// 删除浏览记录
			async delBrowser(id) {
				let res = await this.$myRequest({
					url: '/delBrowser',
					methods: 'POST',
					data: {
						token: uni.getStorageSync('token'),
						ids: id
					}
				})
				if (res.data.status == 1) {
					// this.$api.msg(res.data.msg)
					uni.showToast({
						title: '删除成功',
						icon: 'none'
					})
					this.page = 1;
					this.getNewData('refresh');
				}
			},
			// 添加商品进入购物车
			async addCart(id) {
				if (this.hasLogin == false) {
					uni.showToast({
						title: '请先登陆',
						icon: 'none'
					})
					return
				}

				// if (parameterId <= 0) {
				// 	this.$api.msg('尚未选择规格！')
				// 	return false
				// }

				let res = await this.$myRequest({
					url: '/addCart',
					methods: 'POST',
					data: {
						token: uni.getStorageSync('token'),
						id: id,
						number: 1
					}
				})

				this.$api.msg(res.data.msg)
				uni.$emit('changeCartList');
			},
		},
	}
</script>

<style lang="scss">
	.hide {
		margin: 20rpx 0 0 180rpx;
	}

	.btn5 {
		color: white;
		background-color: #29ad97;
		padding: 10rpx;
		margin-left: 30rpx;
		margin-top: 60rpx;
		border-radius: 50rpx;
		font-size: 26rpx;
		text-align: center;
		width: 160rpx;

	}

	page {
		background-color: #f0efee;
	}

	.ls {
		display: flex;
		flex-direction: column;
		align-items: center;
		position: absolute;
		top: 25%;

		.btn {
			color: white;
			background-color: #29ad97;
			margin-top: 45upx;
			width: 320rpx;
			border-radius: 50rpx;
		}
	}

	.bg {
		width: 96%;
		height: 100%;
		background-color: white;
		margin: 20rpx auto;
		border-radius: 10rpx;
	}

	.list {
		margin-bottom: 20rpx;
		margin-top: 30rpx;
		border-radius: 20rpx;

		.list-image {
			margin-left: 20rpx;
		}
	}

	.list-text {
		font-size: 28rpx;
		margin: 20rpx 0 0 20rpx;
		letter-spacing: 4rpx;
		width: 300rpx;
	}

	.Subheading {
		color: #999;
		margin-top: 10rpx;

	}

	.price {
		font-size: 28rpx;
		flex: 1;
		color: #29ad97;
	}

	.title {
		font-weight: 600;
		white-space: nowrap; //单词在空格处：不换行
		overflow: hidden; //内容溢出的话：隐藏
		text-overflow: ellipsis; //溢出的文本用省略号代替
	}
</style>
